/********************************************************************
* Thank friend GG_ruler for his help 
* Thank friend GG_ruler for his help 
* Thank friend GG_ruler for his help 
* Thank friend GG_ruler for his help 
* @brief		    
    This file is used to write programs related to initialization
* @author           Chuyan

********************************************************************/
#include "Init.h"

/*
Timer 1:    unused                                      *****Timer 1*****unused
Timer 2:    Steering gear PWM channel 1,  Brushless motor PWM channel 3
Timer 3:    Hall encoder channel
Timer 4:    unused                                      *****Timer 4*****unused
Timer 5:    unused                                      *****Timer 5*****unused
Timer 6:    VOFA upper computer timer
Timer 7:    Timer for gyroscope, steering gear, etc     中断优先级 0
Timer 8:    unused                                      *****Timer 8*****unused

UART 1:     debug uart, unused and has been reserved    *****Uart 1*****unused
UART 2:     pins are occupied
UART 3:     GPS urat            无中断优先级
UART 4:     wireless uart
UART 5:     Bluetooth remote control uart
UART 6:     WT gyroscope uart                           中断优先级 2
UART 7:     unused and has been reserved                *****Uart 7*****unused
UART 8:     Camera uart                                 *****Uart 8*****unused*/
//////////////////////// Temporary variable ////////////////////////
char Go = 1, Flag_auto_check = 0, flag_updata;
char STATE_auto_get = 0;
float time_i = 0, time_j, time_k;
// GPS filtering, temporary  variable
float lat,lon,dir;
// Take the nearest GPS point, temporary variable
int16 temporally_i = 0;
float mini_dis = 0;
//////////////////////// Adjustable variable ////////////////////////
float V_duty = 40;
float Esp_ang;

void All_Init()
{    
    clock_init(SYSTEM_CLOCK_120M);
    system_delay_ms(500); // 上电延时 给电容充电
    //    timer_init(TIM_4, TIMER_US);
    tft180_init();		
    //    ips200_init(IPS200_TYPE_PARALLEL8);
    
    //Key initialization
    show_str(0, 0,"key and buzzer");
    gpio_init(LED1, GPO, GPIO_LOW, GPO_PUSH_PULL);		// 
    gpio_init(KEY0, GPI, GPIO_HIGH, GPI_PULL_UP);		// 
    gpio_init(KEY1, GPI, GPIO_HIGH, GPI_PULL_UP);		// 
    gpio_init(KEY2, GPI, GPIO_HIGH, GPI_PULL_UP);		//  
    gpio_init(KEY3, GPI, GPIO_HIGH, GPI_PULL_UP);		//  
    gpio_init(SWITCH_1, GPI, GPIO_HIGH, GPI_PULL_UP);		//  
    gpio_init(SWITCH_2, GPI, GPIO_HIGH, GPI_PULL_UP);		//  
    gpio_init(BUZZER_PIN, GPO, GPIO_LOW, GPO_PUSH_PULL);	//
    show_str(0, 0,"              ");
    
    //GPS initialization
    show_str(0, 0,"GPS"); 
    gps_init();                                             // GPS     
    GPS_inf_init();                                         // GPS 
    show_str(0, 0,"              ");
    
    // brushless initialization
    show_str(0, 0,"MOTOR");
    pwm_init(MOTOR_PWM_CH, 1000, 0);                        // 
    gpio_init(MOTOR_DIR_CH, GPO, GPIO_HIGH, GPO_PUSH_PULL); // 	
    show_str(0, 0,"              ");
    
    //servo initialization
    show_str(0, 0,"SERVO AND PID");
    pwm_init(SERVO_PWM, 100, SERVO_MEDIAN);   //  
    PID_Iint(&Gyro_pid, &Servo_gyro_pid, &Servo_Angle_pid);	                        //  
    show_str(0, 0,"              ");
    
    //upper machine uart initialization
    show_str(0, 0,"VOFA");
    uart_init(VOFA_UART,115200,VOFA_UART_TX, VOFA_UART_RX); // vofa uart initialization
    interrupt_set_priority(VOFA_UART_PRIORITY, VOFA_priority);          // vofa uart set priority 7
    show_str(0, 0,"              ");
    
    //WT-gyro uart initialization
    show_str(0, 0,"WIT-GYRO");
    WIT_inf_init(&WIT);                                     // WIT information initialization
    show_str(0, 0,"              ");
    
    //    //TOF uart initialization
    //    show_str(0, 0,"TOF");
    //    TOFinit(&TOF);                                    // TOF initialization
    //    show_str(0, 0,"              ");
    
    //BULETOOTH uart initialization
    show_str(0, 0,"Remote control");
    //if(0) Bluetooth_init(&RC);                                    // BULETOOTH initialization
    //sbus_init(UART_5, UART5_TX_A4, UART5_RX_A5, UART5_IRQn); // SBUS initialization
    uart5_sbus();
    show_str(0, 0,"              ");
    
    //timer initialization
    show_str(0, 0,"TIM");
    pit_init_ms(TIM6_PIT, 2);             //100MS  GPS  
    interrupt_set_priority(TIM6_IRQn, TIM6_priority);   //priority 4
    
    pit_init_ms(TIM7_PIT, 5);               //5MS  servo\ motion analysis\  motor pwm updata 
    interrupt_set_priority(TIM7_IRQn, TIM7_priority);   //priority 0   
    
    pit_init_ms(TIM5_PIT, 20);             //100MS  GPS  
    interrupt_set_priority(TIM5_IRQn, TIM5_priority);   //priority 4
    show_str(0, 0,"              ");
    
    //brush motor encoder initialization
    show_str(0, 0,"HALL ENCODER");
    encoder_init_dir(ENCODER_TIM, ENCODER_PLUS, ENCODER_DIR);   // 111111111111111111111111111
    show_str(0, 0,"              ");
    
    ring(50);//All initialization is complete
    //    uart_sbus_init();		                        	
    //    pit_init_ms(TIM_4, 10, 2);
}
